
// src/main/java/com/xxxy/ljh/myapplication/DaoImpl/SignupDaoImpl.java
package com.xxxy.ljh.myapplication.DaoImpl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xxxy.ljh.myapplication.Dao.SignupDao;
import com.xxxy.ljh.myapplication.dphelper.sqliteDBHelper;
import com.xxxy.ljh.myapplication.model.Signup;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动报名数据访问接口实现类
 * 提供活动报名相关的数据库操作功能，包括报名、查询、更新等操作
 */
public class SignupDaoImpl implements SignupDao {

    private Context mContext;
    private SQLiteDatabase mDatabase;

    /**
     * 构造函数，初始化数据库连接
     * @param context 应用上下文对象
     */
    public SignupDaoImpl(Context context) {
        mContext = context;
        mDatabase = new sqliteDBHelper(mContext).getWritableDatabase();
    }

    /**
     * 为指定活动进行报名
     * 将报名信息插入到activity_signup表中
     * @param signup 报名信息对象，包含活动ID、用户ID等相关信息
     * @return 返回插入操作的结果，成功返回行ID，失败返回-1
     */
    @Override
    public long signupForActivity(Signup signup) {
        ContentValues values = new ContentValues();
        values.put("activity_id", signup.getActivityId());
        values.put("user_id", signup.getUserId());
        values.put("signup_info", signup.getSignupInfo());
        values.put("signup_status", signup.getSignupStatus());
        values.put("attendance_status", signup.getAttendanceStatus());
        values.put("points", signup.getPoints());
        values.put("award_level", signup.getAwardLevel());
        values.put("award_remark", signup.getAwardRemark());

        long result = mDatabase.insert("activity_signup", null, values);
        return result;
    }

    /**
     * 检查用户是否已报名指定活动
     * 通过查询activity_signup表判断是否存在对应的报名记录
     * @param activityId 活动ID
     * @param userId 用户ID
     * @return 如果已报名返回true，否则返回false
     */
    @Override
    public boolean hasSignedUp(int activityId, int userId) {
        String selection = "activity_id = ? AND user_id = ?";
        String[] selectionArgs = {String.valueOf(activityId), String.valueOf(userId)};
        Cursor cursor = mDatabase.query("activity_signup", null, selection, selectionArgs, null, null, null);
        boolean result = cursor.getCount() > 0;
        cursor.close();
        return result;
    }

    /**
     * 根据活动ID获取所有报名信息
     * 查询指定活动的所有报名记录并封装为Signup对象列表
     * @param activityId 活动ID
     * @return 返回该活动的所有报名信息列表
     */
    @Override
    public List<Signup> getSignupsByActivityId(int activityId) {
        String selection = "activity_id = ?";
        String[] selectionArgs = {String.valueOf(activityId)};
        Cursor cursor = mDatabase.query("activity_signup", null, selection, selectionArgs, null, null, null);

        List<Signup> signups = new ArrayList<>();
        // 遍历查询结果，将每条记录转换为Signup对象
        while (cursor.moveToNext()) {
            Signup signup = new Signup();
            signup.setSignupId(cursor.getInt(cursor.getColumnIndexOrThrow("signup_id")));
            signup.setActivityId(cursor.getInt(cursor.getColumnIndexOrThrow("activity_id")));
            signup.setUserId(cursor.getInt(cursor.getColumnIndexOrThrow("user_id")));
            signup.setSignupInfo(cursor.getString(cursor.getColumnIndexOrThrow("signup_info")));
            signup.setSignupStatus(cursor.getInt(cursor.getColumnIndexOrThrow("signup_status")));
            signup.setReviewTime(cursor.getString(cursor.getColumnIndexOrThrow("review_time")));
            signup.setReviewerId(cursor.getInt(cursor.getColumnIndexOrThrow("reviewer_id")));
            signup.setReviewRemark(cursor.getString(cursor.getColumnIndexOrThrow("review_remark")));
            signup.setSignupTime(cursor.getString(cursor.getColumnIndexOrThrow("signup_time")));
            signup.setAttendanceStatus(cursor.getInt(cursor.getColumnIndexOrThrow("attendance_status")));
            signup.setPoints(cursor.getInt(cursor.getColumnIndexOrThrow("points")));
            signup.setAwardLevel(cursor.getInt(cursor.getColumnIndexOrThrow("award_level")));
            signup.setAwardRemark(cursor.getString(cursor.getColumnIndexOrThrow("award_remark")));
            signups.add(signup);
        }
        cursor.close();
        return signups;
    }

    /**
     * 根据用户ID获取所有报名信息
     * 查询指定用户的所有报名记录并封装为Signup对象列表
     * @param userId 用户ID
     * @return 返回该用户的所有报名信息列表
     */
    @Override
    public List<Signup> getSignupsByUserId(int userId) {
        String selection = "user_id = ?";
        String[] selectionArgs = {String.valueOf(userId)};
        Cursor cursor = mDatabase.query("activity_signup", null, selection, selectionArgs, null, null, null);

        List<Signup> signups = new ArrayList<>();
        // 遍历查询结果，将每条记录转换为Signup对象
        while (cursor.moveToNext()) {
            Signup signup = new Signup();
            signup.setSignupId(cursor.getInt(cursor.getColumnIndexOrThrow("signup_id")));
            signup.setActivityId(cursor.getInt(cursor.getColumnIndexOrThrow("activity_id")));
            signup.setUserId(cursor.getInt(cursor.getColumnIndexOrThrow("user_id")));
            signup.setSignupInfo(cursor.getString(cursor.getColumnIndexOrThrow("signup_info")));
            signup.setSignupStatus(cursor.getInt(cursor.getColumnIndexOrThrow("signup_status")));
            signup.setReviewTime(cursor.getString(cursor.getColumnIndexOrThrow("review_time")));
            signup.setReviewerId(cursor.getInt(cursor.getColumnIndexOrThrow("reviewer_id")));
            signup.setReviewRemark(cursor.getString(cursor.getColumnIndexOrThrow("review_remark")));
            signup.setSignupTime(cursor.getString(cursor.getColumnIndexOrThrow("signup_time")));
            signup.setAttendanceStatus(cursor.getInt(cursor.getColumnIndexOrThrow("attendance_status")));
            signup.setPoints(cursor.getInt(cursor.getColumnIndexOrThrow("points")));
            signup.setAwardLevel(cursor.getInt(cursor.getColumnIndexOrThrow("award_level")));
            signup.setAwardRemark(cursor.getString(cursor.getColumnIndexOrThrow("award_remark")));
            signups.add(signup);
        }
        cursor.close();
        return signups;
    }

    /**
     * 更新报名状态
     * 根据报名ID更新对应的报名状态字段
     * @param signupId 报名ID
     * @param status 新的报名状态值
     * @return 更新成功返回true，失败返回false
     */
    @Override
    public boolean updateSignupStatus(int signupId, int status) {
        ContentValues values = new ContentValues();
        values.put("signup_status", status);

        String whereClause = "signup_id = ?";
        String[] whereArgs = {String.valueOf(signupId)};

        int result = mDatabase.update("activity_signup", values, whereClause, whereArgs);
        return result > 0;
    }

    /**
     * 更新考勤状态
     * 根据报名ID更新对应的考勤状态字段
     * @param signupId 报名ID
     * @param attendanceStatus 新的考勤状态值
     * @return 更新成功返回true，失败返回false
     */
    @Override
    public boolean updateAttendanceStatus(int signupId, int attendanceStatus) {
        ContentValues values = new ContentValues();
        values.put("attendance_status", attendanceStatus);

        String whereClause = "signup_id = ?";
        String[] whereArgs = {String.valueOf(signupId)};

        int result = mDatabase.update("activity_signup", values, whereClause, whereArgs);
        return result > 0;
    }

    /**
     * 根据报名ID获取报名信息
     * 查询指定报名ID的详细信息并封装为Signup对象
     * @param signupId 报名ID
     * @return 返回对应的Signup对象，如果不存在则返回null
     */
    @Override
    public Signup getSignupById(int signupId) {
        String selection = "signup_id = ?";
        String[] selectionArgs = {String.valueOf(signupId)};
        Cursor cursor = mDatabase.query("activity_signup", null, selection, selectionArgs, null, null, null);

        Signup signup = null;
        // 如果查询到记录，则将其转换为Signup对象
        if (cursor.moveToFirst()) {
            signup = new Signup();
            signup.setSignupId(cursor.getInt(cursor.getColumnIndexOrThrow("signup_id")));
            signup.setActivityId(cursor.getInt(cursor.getColumnIndexOrThrow("activity_id")));
            signup.setUserId(cursor.getInt(cursor.getColumnIndexOrThrow("user_id")));
            signup.setSignupInfo(cursor.getString(cursor.getColumnIndexOrThrow("signup_info")));
            signup.setSignupStatus(cursor.getInt(cursor.getColumnIndexOrThrow("signup_status")));
            signup.setReviewTime(cursor.getString(cursor.getColumnIndexOrThrow("review_time")));
            signup.setReviewerId(cursor.getInt(cursor.getColumnIndexOrThrow("reviewer_id")));
            signup.setReviewRemark(cursor.getString(cursor.getColumnIndexOrThrow("review_remark")));
            signup.setSignupTime(cursor.getString(cursor.getColumnIndexOrThrow("signup_time")));
            signup.setAttendanceStatus(cursor.getInt(cursor.getColumnIndexOrThrow("attendance_status")));
            signup.setPoints(cursor.getInt(cursor.getColumnIndexOrThrow("points")));
            signup.setAwardLevel(cursor.getInt(cursor.getColumnIndexOrThrow("award_level")));
            signup.setAwardRemark(cursor.getString(cursor.getColumnIndexOrThrow("award_remark")));
        }
        cursor.close();
        return signup;
    }

    /**
     * 获取指定活动的报名人数
     * 统计指定活动ID的报名记录数量
     * @param activityId 活动ID
     * @return 返回该活动的报名人数
     */
    @Override
    public int getSignupCount(int activityId) {
        String selection = "activity_id = ?";
        String[] selectionArgs = {String.valueOf(activityId)};
        Cursor cursor = mDatabase.query("activity_signup", null, selection, selectionArgs, null, null, null);
        int count = cursor.getCount();
        cursor.close();
        return count;
    }
}